package com.example.kliven.because.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.kliven.because.R;
import com.example.kliven.because.Singleton.VolleySingleton;
import com.example.kliven.because.adapter.InterestingAdapter;
import com.example.kliven.because.bean.InterestingBean;
import com.example.kliven.because.intentbean.NetFashionBean;
import com.example.kliven.because.intentbean.NetInterestingBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kliven on 16/1/11.
 */
public class InterestingFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
    private ListView listView;
    private List<InterestingBean> interestingBeans;
    private SwipeRefreshLayout swipeRefreshLayout;
    private InterestingAdapter interestingAdapter;
    private String url = "http://zzd.sm.cn/appservice/api/v1/channel/10013?client_os=android&client_version=2.1.0.1&bid=800&m_ch=005&city=0411&sn=eda7198b96b6b6239a7f5b39114635e0&content_cnt=20&recoid=14571511743984622251&ftime=1452848926351&method=new&count=20";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.intersting_fragment, null);
        listView = (ListView) view.findViewById(R.id.list_view);
        listView.setOnItemClickListener(this);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
        VolleySingleton.getInstance();
        getNetData();
        return view;
    }

    private void getNetData() {
        VolleySingleton.addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                interestingBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetInterestingBean netInterestingBean = gson.fromJson(response, NetInterestingBean.class);
                for (int i = 0; i < netInterestingBean.getData().getArticle().size(); i++) {
                    InterestingBean interestingBean = new InterestingBean();
                    interestingBean.setSummary(netInterestingBean.getData().getArticle().get(i).getSummary());
                    for (int j = 0; j < netInterestingBean.getData().getArticle().get(i).getImage().size(); j++) {
                        interestingBean.setMain_img_url(netInterestingBean.getData().getArticle().get(i).getImage().get(j).getUrl());
                    }
                    interestingBeans.add(interestingBean);
                }
                interestingAdapter = new InterestingAdapter(interestingBeans, getActivity());
                listView.setAdapter(interestingAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
                getNetData();
            }
        }, 5000);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent interestIntent = new Intent();
    }
}
